package cn.befory.service.impl;

import cn.befory.constants.CommonConstants;
import cn.befory.constants.PermContants;
import cn.befory.dao.PermMapper;
import cn.befory.pojo.Perm;
import cn.befory.service.PermService;
import cn.befory.service.vo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by befory on 2019/10/14.
 */
@Service("permService")
public class PermServiceImpl implements PermService {
    @Autowired
    private PermMapper permMapper;
    @Override
    public List<Menu> getMenus(Long uid) throws Exception {
        //获取菜单
        List<Perm> perms = permMapper.selectAll(uid);
        List<Menu> menus = new ArrayList<>();
        //处理一级菜单
        for (Perm perm : perms){
            if(perm.getType() == PermContants.Perm.MENU.getType()
                    && perm.getParentId() == CommonConstants.ZERO){
                menus.add(new Menu().setId(perm.getId())
                        .setFlag(perm.getFlag())
                        .setIcon(perm.getIcon())
                        .setName(perm.getName())
                        .setUrl(perm.getUrl()));
            }
        }
        for (Perm perm : perms){
            if(perm.getType() == PermContants.Perm.MENU.getType()
                    && perm.getParentId() != CommonConstants.ZERO){
                addSecondMenu(menus,new Menu().setId(perm.getId())
                        .setFlag(perm.getFlag())
                        .setIcon(perm.getIcon())
                        .setName(perm.getName())
                        .setUrl(perm.getUrl()),perm.getParentId());
            }
        }
        return menus;
    }

    @Override
    public List<Perm> getMenus(Long uid, String param) throws Exception {
        //获取菜单
        List<Perm> perms = permMapper.selectAll(uid);
        if(PermContants.PermRank.PARENT.getName().equals(param)){
            return perms.stream()
                    .filter(perm ->
                            (perm.getType() == PermContants.Perm.MENU.getType()
                                    && perm.getParentId() == CommonConstants.ZERO))
                    .collect(Collectors.toList());
        }
        return perms;
    }

    @Override
    public boolean addPerm(Perm perm) throws Exception {
        return false;
    }

    private boolean addSecondMenu(List<Menu> menus,Menu child,Long parentId)throws Exception{
        if(parentId == CommonConstants.ZERO){
            return false;
        }
        for (Menu menu : menus){
            if(menu.getId().equals(parentId)){
                menu.setHasChildren(true).add(child);
            }
        }
        return true;
    }


//    public static void main(String[] args) {
//        List<Menu> menus = new ArrayList<>();
//        Menu menu = new Menu().setId(1L).setFlag("d").setHasChildren(true).setIcon("f").setUrl("f");
//        Menu menu2 = new Menu().setId(2L).setFlag("dd").setHasChildren(true).setIcon("f").setUrl("f");
//        menu.add(menu2);
//        menus.add(menu);
//
//
//        menus.forEach(menu1 -> {
//            System.out.println(menu1.getId());
//            List<Menu> childList = menu1.getChildList();
//            System.out.println(childList.size());
//        });
//        System.out.println(System.currentTimeMillis());
//    }
}
